﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dataweb.NShape;

namespace cz.zcu.PreSti.Guide
{
    /// <summary>
    /// Provides template to create a specific guide.
    /// </summary>
    public abstract class GuideParent
    {
        //Constructor parameters
        protected Project project;
        protected Diagram diagram;
        protected List<Property> listProperties;
        protected Form window;

        //Variables
        protected SymbolsCreater symbolsCreater;

        //Lists
        protected List<TemplatesShape> listShapes;
        protected List<TemplatesShape> listTriggers;

        #region WindowParametrs

        //Controls
        protected ListBox listbox;
        protected CheckBox checkBox;
        protected ComboBox cbAlignmentH;
        protected ComboBox cbAlignmentV;
        protected ComboBox cbStimuliType;

        //Buttons
        protected Button btCreateGraph;
        protected Button btAdd;
        protected Button btPath;
        protected Button btNext;
        protected Button btDelete;
        protected Button btPrevious;
        protected Button btRun;

        //Labels
        protected Label lbText;
        protected Label lbSubtitle;
        protected Label lbHelpText1;
        protected Label lbHelpText2;
        protected Label lbNoStimulus;
        protected Label lbTitle;

        //GroupBoxes
        protected GroupBox gbStimuli;
        protected GroupBox gbInformation;
        protected GroupBox gbHelp;
        protected GroupBox gbProperties;

        #endregion

        /// <summary>
        /// Initializes the parametres from the presentation layer.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <param name="diagram">The diagram.</param>
        /// <param name="listProperties">The list properties.</param>
        /// <param name="window">The window.</param>
        protected void InitializeParametres(Project project, Diagram diagram, List<Property> listProperties, Form window)
        {
            //Initialization
            this.project = project;
            this.diagram = diagram;
            this.listProperties = listProperties;
            this.window = window;

            //Information 
            this.gbInformation = (GroupBox)window.Controls["GBInformation"];
            this.lbText = (Label)window.Controls["GBInformation"].Controls["LBText"];
            this.checkBox = (CheckBox)window.Controls["GBInformation"].Controls["CBHelp"];
            this.cbAlignmentH = (ComboBox)window.Controls["GBProperties"].Controls["CBAlignmentH"];
            this.cbAlignmentV = (ComboBox)window.Controls["GBProperties"].Controls["CBAlignmentV"];

            //Properties
            this.gbProperties = (GroupBox)window.Controls["GBProperties"];
            this.btPath = (Button)window.Controls["GBProperties"].Controls["BTPath"];
            this.lbNoStimulus = (Label)window.Controls["GBProperties"].Controls["LBNoStimulus"];

            //Add stimuli
            this.gbStimuli = (GroupBox)window.Controls["GBStimuli"];
            this.btAdd = (Button)window.Controls["GBStimuli"].Controls["BTAdd"];
            this.listbox = (ListBox)window.Controls["GBStimuli"].Controls["listBox1"];
            this.btDelete = (Button)window.Controls["GBStimuli"].Controls["BTDelete"];
            this.cbStimuliType = (ComboBox)window.Controls["GBStimuli"].Controls["CBStimuliType"];

            //Help
            this.gbHelp = (GroupBox)window.Controls["GBHelp"];
            this.lbHelpText1 = (Label)window.Controls["GBHelp"].Controls["flowLayoutPanel1"].Controls["LBHelpText1"];
            this.lbHelpText2 = (Label)window.Controls["GBHelp"].Controls["flowLayoutPanel1"].Controls["LBHelpText2"];

            //Without groupbox
            this.lbSubtitle = (Label)window.Controls["LBSubtitle"];
            this.btNext = (Button)window.Controls["BTNext"];
            this.btCreateGraph = (Button)window.Controls["BTCreateGraph"];
            this.btPrevious = (Button)window.Controls["BTPrevious"];
            this.lbTitle = (Label)window.Controls["LBTitle"];
            this.btRun = (Button)window.Controls["BTRun"];

            //Lists Initialization
            symbolsCreater = new SymbolsCreater(this.project);
            listShapes = new List<TemplatesShape>();
            listTriggers = new List<TemplatesShape>();
        }

        /// <summary>
        /// Adds the static part of a graph. It's fixed part. An user can't change that .
        /// </summary>
        protected abstract void AddStaticGraphPart();
        /// <summary>
        /// Adds the dynamic part of a graph It's optional part. An user can change this graph part during working with the application  .
        /// </summary>
        protected abstract void AddDynamicGraphPart();

        /// <summary>
        /// Creates the graph.
        /// </summary>
        public void CreateGraph()
        {
            AddStaticGraphPart();
            AddDynamicGraphPart();
        }

        /// <summary>
        /// Adds the stimuli to the listbox and to the stimuli list .
        /// </summary>
        /// <param name="stimuli">A stimuli type.</param>
        protected void AddStimuli(String stimuli)
        {
            if (stimuli == "Text")
            {
                listShapes.Add(symbolsCreater.AddTextStimulus());
                listbox.Items.Add("Text");
            }
            if (stimuli == "Image")
            {
                listShapes.Add(symbolsCreater.AddImageStimulus());
                listbox.Items.Add("Image");
            }
            if (stimuli == "Sound")
            {
                listShapes.Add(symbolsCreater.AddSoundStimulus());
                listbox.Items.Add("Sound");
            }
            btCreateGraph.Enabled = true;
            btRun.Enabled = true;
        }

        /// <summary>
        /// Connects two shapes with trigger, that is placed in the triggers list.
        /// </summary>
        /// <param name="shape1">The shape1.</param>
        /// <param name="shape2">The shape2.</param>
        /// <param name="point1">The point1: glue point of shape.</param>
        /// <param name="point2">The point2: glue point of shape.</param>
        protected void ConnectTriggShapes(Shape shape1, Shape shape2, int point1, int point2)
        {
            listTriggers[listTriggers.Count - 1].Shape.Connect(1, shape1, point1);
            listTriggers[listTriggers.Count - 1].Shape.Connect(2, shape2, point2);
        }

        /// <summary>
        /// Unvissibles the items of presentation layer.
        /// </summary>
        /// <param name="lastIndex">The last index: index of last (textbox,label).</param>
        protected void UnvissibleItems(int lastIndex)
        {
            int firstIndex = lastIndex - 1;
            for (int i = firstIndex; i < listProperties.Count; i++)
            {
                listProperties[i].Label.Visible = false;
                listProperties[i].TextBox.Visible = false;
            }
        }

        /// <summary>
        /// Vissibles the items.
        /// </summary>
        /// <param name="index">The index: index of last (textbox,label).</param>
        protected void VissibleItems(int index)
        {
            int lastIndex = index + 1;
            for (int i = 0; i < lastIndex; i++)
            {
                listProperties[i].Label.Visible = true;
                listProperties[i].TextBox.Visible = true;
            }
        }
    }
}
